% Reshaping UK data output from Stata for Matlab input
%clear all
load ../data/DataforMatlab/UK_Budget_Raw.csv
UK_Budget_Raw(:,1) = [];
UK_Budget_Raw(:,2) = [];

rpimaster_reg_mat = UK_Budget_Raw;


%%
I = 17;
NL = 10000;
N = 4900;%max samples for panel
T = 2017- 1974 + 1;

B_vec_L = zeros(I,NL,T);
w_vec_L = zeros(1,NL,T);
B_vec = zeros(I,N,T);
I_vec = zeros(1,N,T);
for t = 1:T 
ind = find(rpimaster_reg_mat(:,1) == 1973+t);

btmp=  rpimaster_reg_mat(min(ind):max(ind),3:end)';
wtmp =  rpimaster_reg_mat(min(ind):max(ind),2)';   
[wtmp2, wLind] = sort(wtmp);
btmp2 = btmp(:,wLind,:);
w_vec_L(1,1:size(ind,1),t) = wtmp2;
w_vec_L(1,size(ind,1):end,t) = NaN;
B_vec_L(:,1:size(ind,1),t) = btmp2;

[v1,indmax] = max(wtmp2);
indmax = min(indmax);

w_vec_L(w_vec_L==0) = NaN;

quantiles = linspace(0, 1, N);

quantile_values = quantile(wtmp2, quantiles);

quantile_indices = zeros(size(quantile_values));

for i = 1:length(quantile_values)
    [~, quantile_indices(i)] = min(abs(wtmp2 - quantile_values(i)));
end
w_vectmp = wtmp2(quantile_indices);
B_vectmp = btmp2(:,quantile_indices);

[w_vecsort, wind] = sort(w_vectmp);
B_vectmp = B_vectmp(:,wind,:);
I_vec(1,:,t) = w_vecsort;
B_vec(:,:,t) = B_vectmp;
end
I_vec = I_vec(:,:,1:end);
B_vec = B_vec(:,:,1:end);
%%
w_vec_long = reshape(log(w_vec_L),[10000,T]);


